package com.itany.boot.nmms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.boot.nmms.constant.DistinctConstant;
import com.itany.boot.nmms.entity.Product;
import com.itany.boot.nmms.entity.ProductType;
import com.itany.boot.nmms.exception.GetSerialNoException;
import com.itany.boot.nmms.exception.ProductExistsException;
import com.itany.boot.nmms.exception.RequestParamsException;
import com.itany.boot.nmms.service.ProductService;
import com.itany.boot.nmms.service.ProductTypeService;
import com.itany.boot.nmms.util.ParamsUtil;
import com.itany.boot.nmms.util.ResponseResult;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 产品控制器
 *
 * @author Tom
 * @date 2021/06/05
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @Autowired
    private ProductTypeService productTypeService;

    /**
     * 显示
     *
     * @param pageNo   页面没有
     * @param pageSize 页面大小
     * @return {@link ModelAndView}
     */
    @RequestMapping("/show")
    public ModelAndView show(String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();
        List<ProductType> types = productTypeService.findAllByStatus();
        mav.addObject("types", types);
        if (ParamsUtil.isNull(pageNo)) {
            pageNo = DistinctConstant.DEFAULT_PAGE;
        }
        if (ParamsUtil.isNull(pageSize)) {
            pageSize = DistinctConstant.DEFAULT_PAGESIZE;
        }
        PageHelper.startPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        List<Product> products = productService.findAll();
        PageInfo<Product> pageInfo = new PageInfo<>(products);
        // 当当前页面没有数据，且不是第一页的时候，去前一页
        if (pageInfo.getList() == null && !"1".equals(pageNo)) {
            int page = Integer.parseInt(pageNo) - 1;
            mav.setViewName("redirect:show?pageNo=" + page);
        } else {
            mav.addObject("pageInfo", pageInfo);
            mav.setViewName("backend/productManager");
        }
        return mav;
    }

    /**
     * 添加
     *
     * @param name        的名字
     * @param price       价格
     * @param productType 产品类型
     * @param file        文件
     * @param session     会话
     * @return {@link ResponseResult}
     */
    @ResponseBody
    @RequestMapping("/add")
    public ResponseResult add(String name, String price, String productType, @RequestParam(value = "image", required = false) MultipartFile file, HttpSession session) {
        ResponseResult result = null;

        try {
            productService.add(name, price, productType, file, session);
            result = ResponseResult.success("成功");
        } catch (RequestParamsException e) {
            result = ResponseResult.fail(e.getMessage());
        } catch (FileUploadException e) {
            result = ResponseResult.fail(e.getMessage());
        } catch (GetSerialNoException e) {
            result = ResponseResult.fail(e.getMessage());
        } catch (ProductExistsException e) {
            result = ResponseResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result = ResponseResult.fail("服务器内部异常");
        }


        return result;
    }

    /**
     * 发现通过id
     *
     * @param id id
     * @return {@link ResponseResult}
     */
    @ResponseBody
    @RequestMapping("/findById")
    public ResponseResult findById(String id) {
        ResponseResult result;
        try {
            Product product = productService.findById(id);
            result = ResponseResult.success(product);
        } catch (RequestParamsException e) {
            result = ResponseResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result = ResponseResult.fail("服务器内部异常");
        }

        return result;
    }

    /**
     * 修改商品
     *
     * @param id          id
     * @param name        的名字
     * @param price       价格
     * @param productType 产品类型
     * @param file        文件
     * @param session     会话
     * @return {@link ResponseResult}
     */
    @ResponseBody
    @RequestMapping("/modifyPro")
    public ResponseResult modifyPro(String id, String name, String price, String productType, @RequestParam(value = "image", required = false) MultipartFile file, HttpSession session) {
        ResponseResult result;
        try {
            productService.update(id, name, price, productType, file, session);
            result = ResponseResult.success();
        } catch (RequestParamsException e) {
            result = ResponseResult.fail(e.getMessage());
        } catch (ProductExistsException e) {
            result = ResponseResult.fail(e.getMessage());
        } catch (FileUploadException e) {
            result = ResponseResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result = ResponseResult.fail("服务器内部异常");
        }


        return result;
    }

    /**
     * 删除商品
     *
     * @param id id
     * @return {@link ResponseResult}
     */
    @ResponseBody
    @RequestMapping("/strikeOut")
    public ResponseResult strikeOut(String id) {
        ResponseResult result;
        try {
            productService.removeById(id);
            result = ResponseResult.success();
        } catch (RequestParamsException e) {
            result = ResponseResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result = ResponseResult.fail("服务器内部异常");
        }

        return result;

    }

}
